package com.itheima.reggie.service.ServiceImp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.DishMapper;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImp extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired//菜品接口
    DishFlavorService dishFlavorService;

    @Override
    @Transactional  // 由于在 saveFlavor 方法中，进行了两次数据库的保存操作，操作了两张表，
                    // 那么为了保证数据的一致性，我们需要在方法上加上注解 @Transactional来控制事务。
    public void saveFlavor(DishDto dishDto) {//只能直接获取name 和 value,
        //缺少dish_id,所以要先获取菜品Id来为dish_Flavor中dish_id赋值
        /*①. 保存菜品基本信息;
        ②. 获取保存的菜品ID;
        ③. 获取菜品口味列表，遍历列表，为菜品口味对象属性dishId赋值;
        ④. 批量保存菜品口味列表;*/

        this.save(dishDto);

        Long dishId = dishDto.getId();//获取的菜品ID

        List<DishFlavor> flavors = dishDto.getFlavors();//获取菜品口味
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());//用Stream流往flavors中塞菜品id变成dish_id

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto getByIdFlavor(Long id) {//菜品修改,需要先根据菜品id查找菜品
        /*A. 根据ID查询菜品的基本信息
        B. 根据菜品的ID查询菜品口味列表数据
        C. 组装数据并返回*/
        Dish dish = this.getById(id);//根据id重新dish基本数据

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);//将活动的菜品信息复制到dishDto中

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();//获取构造器
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        //根据dish_id从dish_flavor表查询当前菜品对应的口味信息
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //将获取的口味信息封装到dishDto
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    public void updateFlavor(DishDto dishDto) {
        this.updateById(dishDto);//更新dish表基本信息

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();//创建构造器
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper); //清理当前菜品对应口味数据---dish_flavor表的delete操作
                                                //就是直接删除对应的菜品口味
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());//用stream流获取更新的信息

        dishFlavorService.saveBatch(flavors);//将获取的新口味信息封装



    }
}
